<?php

namespace app\admin\controller;

use think\Db;
use think\Session;


class Login extends \think\Controller
{

    public function login()
    {
        if (request()->isPost()) {
            $captcha = new \think\captcha\Captcha();
            $data = input('post.');
            if (!$captcha->check($data['code'])) {
                $this->error('验证码错误');
            } else {
                $res = Db::name('cn_service_admin')->where('admin_account', $data['admin_account'])->find();
                if (empty($res) || $res['admin_password'] != md5($data['admin_password'])) {
                    $this->error("用户名或密码错误");
                } else {
                    Session::set('admin_name', $res['admin_account']);
                    $this->success('登录成功', '/admin/Service/index');
                }
            }
        } else {
            return view('login/login');
        }
    }


    public function logout()
    {
        Session::delete('admin_name');
        return view('login/login');
    }


    //个人测试增加账号使用
    public function addAccount()
    {
        $account = $this->request->param('account');
        $password = md5($this->request->param('password'));
        $sevice_admin_info = db::table('cn_service_admin')->where('admin_account', $account)->find();
        if (empty($sevice_admin_info)) {
            $sevice_admin_info = db::table('cn_service_admin')->insert(['service_admin_id' => time(), 'admin_account' => $account, 'admin_password' => $password]);
            return json(['status' => 200, 'msg' => lang('成功'), 'data' => '']);
        } else {
            return json(['status' => 30006, 'msg' => lang('该用户已经存在'), 'data' => '']);
        }
    }
}
